﻿using System.Threading;
using Core.Singleton;
using UnityEngine;

namespace Core.Interfaces
{
    /// <summary>
    /// CancelToken拓展，可以自动回收
    /// </summary>
    public abstract class CancelTokenExtensionMono : MonoBehaviour
    {
        protected CancellationTokenSource TokenSource = new CancellationTokenSource();

        public virtual void OnInit()
        {
        }

        /// <summary>
        /// 在对象销毁时，自动回收
        /// 事件，计时器等
        /// </summary>
        public virtual void OnDestroy()
        {
            //相当于设置CancellationToken.IsCancellationRequested =true;
            TokenSource.Cancel();
            //循环将所有的=true的通知取消
            KKGL.CancelTokenMgr.OnCancelTokenNotify();
            TokenSource.Dispose();
        }
    }


    public abstract class CancelTokenExtension
    {
        protected CancellationTokenSource TokenSource = new CancellationTokenSource();

        public virtual void OnInit()
        {
        }

        /// <summary>
        /// 在对象销毁时，自动回收
        /// 事件，计时器等
        /// </summary>
        public virtual void OnDestroy()
        {
            //相当于设置CancellationToken.IsCancellationRequested =true;
            TokenSource.Cancel();
            //循环将所有的=true的通知取消
            KKGL.CancelTokenMgr.OnCancelTokenNotify();
            TokenSource.Dispose();
        }
    }
}